//
//  KRAlbumInfoCell.m
//  cookBook
//
//  Created by kuroken on 16/1/21.
//  Copyright © 2016年 kuroken. All rights reserved.
//

#import "KRAlbumInfoCell.h"
#import <PureLayout.h>
#import <UIImageView+WebCache.h>
#import <UIButton+WebCache.h>
#import <SDWebImageManager.h>
#import "KRConst.h"
#import "KRAlbumInfo.h"

// cell的标识
static NSString * const albumInfoID = @"albumInfo";

@interface KRAlbumInfoCell ()
/** 专辑封面 imageView*/
@property (nonatomic, weak) UIImageView *albumCoverImage;
/** 专辑标题 label*/
@property (nonatomic, weak) UILabel *albumTitleLabel;
/** 作者用户名 label*/
@property (nonatomic, weak) UILabel *albumUserNameLabel;
/** 作者头像 button*/
@property (nonatomic, weak) UIButton *albumAvatarBtn;
/** 专辑介绍 label*/
@property (nonatomic, weak) UILabel *albumContentLabel;

@property (nonatomic, weak) UIView *coverView;
@end

@implementation KRAlbumInfoCell

+ (instancetype)cellWithTableView:(UITableView *)tableView
{
    KRAlbumInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:albumInfoID];
    if (!cell) {
        cell = [[KRAlbumInfoCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:albumInfoID];
    }
    return cell;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        if (!iOS8) {  //iOS7
            self.contentView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
        }

        self.clipsToBounds = YES;
//        self.contentView.backgroundColor = [UIColor redColor];
        self.selectionStyle = UITableViewCellSelectionStyleNone;

        [self setupSubview];
        [self addConstraint];
        
    }
    return self;
}

- (void)setAlbumInfo:(KRAlbumInfo *)albumInfo
{
    _albumInfo = albumInfo;
    
    [self.albumCoverImage sd_setImageWithURL:[NSURL URLWithString:albumInfo.albumCover]];
    [self.albumTitleLabel setText:albumInfo.albumTitle];
    [self.albumAvatarBtn sd_setImageWithURL:[NSURL URLWithString:albumInfo.albumAvatarUrl] forState:UIControlStateNormal];
    [self.albumUserNameLabel setText:albumInfo.albumUserName];
    [self.albumContentLabel setText:albumInfo.albumContent];
}

- (UIImage *)blurryImage:(UIImage *)image withBlurLevel:(CGFloat)blur{
    CIImage *inputImage = [CIImage imageWithCGImage:image.CGImage];
    CIFilter *filter = [CIFilter filterWithName:@"CIGaussianBlur" keysAndValues:kCIInputImageKey,inputImage,@"inputRadius",@(blur),nil];
    CIImage *outPutImage = filter.outputImage;
    CIContext *context = [CIContext contextWithOptions:nil];
    CGImageRef outImage = [context createCGImage:outPutImage fromRect:[inputImage extent]];
    UIImage *theImage = [UIImage imageWithCGImage:outImage];
    CFRelease(outImage);
    return theImage;
}

/**
 *  初始化自控件
 */
- (void)setupSubview
{
    /** 专辑封面 imageView*/
    UIImageView *albumCoverImage = [[UIImageView alloc] init];
    albumCoverImage.contentMode = UIViewContentModeScaleAspectFill;
    albumCoverImage.clipsToBounds = YES;
    [self.contentView addSubview:albumCoverImage];
    self.albumCoverImage = albumCoverImage;
    
    UIView *coverView = [[UIView alloc] init];
    coverView.backgroundColor = KRColorWithAlpha(100, 100, 100, 0.2);
    [self.albumCoverImage addSubview:coverView];
    self.coverView = coverView;
    
    /** 专辑标题 label*/
    UILabel *albumTitleLabel = [[UILabel alloc] init];
    albumTitleLabel.numberOfLines = 0;
    [albumTitleLabel setTextColor:[UIColor whiteColor]];
    [albumTitleLabel setFont:[UIFont boldSystemFontOfSize:20]];
    [albumTitleLabel setTextAlignment:NSTextAlignmentCenter];
    [self.contentView addSubview:albumTitleLabel];
    self.albumTitleLabel = albumTitleLabel;

    /** 作者用户名 label*/
    UILabel *albumUserNameLabel = [[UILabel alloc] init];
    [albumUserNameLabel setTextColor:[UIColor whiteColor]];
    [albumUserNameLabel setFont:[UIFont systemFontOfSize:18]];
    [self.contentView addSubview:albumUserNameLabel];
    self.albumUserNameLabel = albumUserNameLabel;
    
    /** 作者头像 button*/
    UIButton *albumAvatarBtn = [[UIButton alloc] init];
    [self.contentView addSubview:albumAvatarBtn];
    self.albumAvatarBtn = albumAvatarBtn;
    
    /** 专辑介绍 label*/
    UILabel *albumContentLabel = [[UILabel alloc] init];
    albumContentLabel.numberOfLines = 0;
    [albumContentLabel setFont:[UIFont systemFontOfSize:18]];
    [albumContentLabel setBackgroundColor:[UIColor clearColor]];
    [self.contentView addSubview:albumContentLabel];
    self.albumContentLabel = albumContentLabel;
}

/**
 *  添加约束
 */
- (void)addConstraint
{
    [self.albumCoverImage autoPinEdgesToSuperviewEdgesWithInsets:UIEdgeInsetsZero excludingEdge:ALEdgeBottom];
    [self.albumCoverImage autoSetDimension:ALDimensionHeight toSize:160];
    
    [self.coverView autoPinEdgesToSuperviewEdges];
    
    [self.albumTitleLabel autoAlignAxisToSuperviewAxis:ALAxisVertical];
    [self.albumTitleLabel autoPinEdgeToSuperviewEdge:ALEdgeTop withInset:30];
    [self.albumTitleLabel autoPinEdgeToSuperviewEdge:ALEdgeLeft withInset:40];
    
    [self.albumAvatarBtn autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:self.albumTitleLabel withOffset:15];
    [self.albumAvatarBtn autoPinEdge:ALEdgeLeft toEdge:ALEdgeLeft ofView:self.albumTitleLabel withOffset:60];
    [self.albumAvatarBtn autoSetDimensionsToSize:CGSizeMake(40, 40)];
    self.albumAvatarBtn.layer.cornerRadius = 40 * 0.5;
    self.albumAvatarBtn.clipsToBounds = YES;
    
    [self.albumUserNameLabel autoAlignAxis:ALAxisHorizontal toSameAxisOfView:self.albumAvatarBtn];
    [self.albumUserNameLabel autoPinEdge:ALEdgeLeft toEdge:ALEdgeRight ofView:self.albumAvatarBtn withOffset:10];
    
    [self.albumContentLabel autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:self.albumCoverImage withOffset:10];
    [self.albumContentLabel autoPinEdgeToSuperviewEdge:ALEdgeLeft withInset:15];
    [self.albumContentLabel autoPinEdgeToSuperviewEdge:ALEdgeRight withInset:15];
    [self.albumContentLabel autoPinEdgeToSuperviewEdge:ALEdgeBottom withInset:10];
    
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
